<template>
  <div class="box">
    <div class="box-header" :title="hintText">
      <span class="title pos-left">{{title}}</span>
      <ul class="tools pos-right">
        <li class="item">
          <button class="btn" type="button" @click="showSettingWindow">
            <i class="icon icon-setting"></i>
          </button>
        </li>
      </ul>
    </div>
    <div class="box-body">
      <div class="chart fullsize" id="chart-remoter-1"></div>
      <div class="chartSetting" v-show="showSetting">
        <div class="form">
          <div class="form-head">
            <div class="title">选择时间范围</div>
          </div>
          <div class="form-body">
            <div class="form-item">
              <div class="label">开始时间</div>
              <div class="field">
                <input
                  type="date"
                  class="datepicker"
                  v-model="startTime"
                  :max="timeLimit"
                  :min="timeMin"
                />
              </div>
            </div>
            <div class="form-item">
              <div class="label">结束时间</div>
              <div class="field">
                <input
                  type="date"
                  class="datepicker"
                  v-model="endTime"
                  :max="timeLimit"
                  :min="timeMin"
                />
              </div>
            </div>
          </div>
          <div class="form-foot">
            <button type="button" @click="saveSetting" class="btn bg-green">
              <span class="text">确定</span>
            </button>
            <button type="button" @click="cancelSetting" class="btn bg-green">
              <span class="text">关闭</span>
            </button>
          </div>
        </div>
      </div>
      <x-loading :show="showLoading"></x-loading>
      <dl-District ref="dialog" :show="showModal" :take="keyModal"></dl-District>
    </div>
  </div>
</template>
<script>
import axios from "axios";
import echarts from "echarts";
import Loading from "@/components/Loading";
import dlDistrict from "@/view/remoter/dl_District";

export default {
  data() {
    return {
      startTime: this.dateShift(0),
      endTime: this.dateShift(0),
      showSetting: false,
      showLoading: false,
      chartInstance: null,
      option: null,
      params: null,
      showModal: false,
      keyModal: null,
      chartData: null,
      autoflip: false,
      ticketfn: null,
      timeLimit: this.today(),
      timeMin: this.dateShift(-10)
    };
  },
  props: {
    title: String
  },
  components: {
    "x-loading": Loading,
    "dl-District": dlDistrict
  },
  mounted() {
    this.renderChart();
  },
  computed: {
    hintText() {
      return `显示数据自${this.startTime}起，截止至${this.endTime}`;
    }
  },
  methods: {
    showSettingWindow() {
      this.showSetting = true;
    },
    cancelSetting() {
      this.showSetting = false;
    },
    saveSetting() {
      this.renderChart();
      this.showSetting = false;
    },
    renderChart() {
      let $this = this;
      this.params = {
        startTime: this.startTime + " 00:00:00",
        endTime: this.endTime + " 23:59:59"
      };
      this.showLoading = true;
      axios
        .get(this.URLHEAD + "IDangerAreaDistribution", {
          params: this.params
        })
        .then(res => {
          let data = res.data;
          var maxValue = this.findMaxValue(data.Data, "value1");
          this.option = {
            tooltip: {
              show: "true",
              axisPointer: { type: "shadow" },
              trigger: "item",
              backgroundColor: "rgba(0,0,0,0.7)", // 背景
              padding: [8, 10], //内边距
              extraCssText: "box-shadow: 0 0 3px rgba(255, 255, 255, 0.4);" //添加阴影
            },
            grid: {
              top: 30,
              left: 20,
              right: 20,
              bottom: 20,
              containLabel: true
            },
            legend: {
              top: "top",
              left: "right",
              itemWidth: 10,
              itemHeight: 10,
              data: ["未产生隐患的单位", "已产生隐患的单位"],
              textStyle: {
                color: "#fff",
                fontSize: 12
              }
            },
            yAxis: {
              type: "category",
              axisLine: {
                color: "#ffffff33"
              },
              axisTick: {
                color: "#ffffff33"
              },
              axisLabel: {
                textStyle: { fontSize: 12, color: "#fff" },
                formatter(val) {
                  return val;
                }
              },
              splitLine: {
                color: "#ffffff10"
              },
              data: data.Data.map(o => {
                return o.name;
              })
            },
            xAxis: {
              type: "value",
              axisLine: {
                color: "#ffffff33"
              },
              axisTick: {
                color: "#ffffff33"
              },
              axisLabel: {
                textStyle: { fontSize: 12, color: "#fff" },
                formatter(val) {
                  return val + "家";
                }
              },
              splitLine: {
                color: "#ffffff10"
              }
            },
            series: [
              {
                name: "已产生隐患的单位",
                stack: "总量",
                type: "bar",
                barWidth: "40%",
                data: data.Data.map(o => {
                  return { name: o.name, value: o.value3, warn: o.value1 };
                }),
                label: {
                  normal: {
                    show: true,
                    position: "top"
                  }
                },
                itemStyle: {
                  normal: {
                    color(params) {
                       let max = maxValue;
                       let red = parseInt((params.data.warn / max) * 255);
                       let green = 255 - red;
                       return `rgb(${red},${green},0)`;
                    }
                  },
                  barBorderRadius: 0,
                  shadowColor: "#00000050",
                  shadowBlur: 10,
                  shadowOffsetX: 5,
                  shadowOffsetY: 5
                }
              },
              {
                name: "未产生隐患的单位",
                stack: "总量",
                type: "bar",
                barWidth: "40%",
                data: data.Data.map(o => {
                  return { name: o.name, value: o.value2 - o.value3 };
                }),
                label: {
                  show: true,
                  position: "top",
                  color: "#fff"
                },
                itemStyle: {
                  barBorderRadius: [0, 10, 10, 0],
                  color: "#ffffff33",
                  shadowColor: "#00000050",
                  shadowBlur: 10,
                  shadowOffsetX: 5,
                  shadowOffsetY: 5
                }
              }
            ]
          };
          console.log(this.option);
          this.chartInstance = this.$Crender("chart-remoter-1", this.option);
          this.chartInstance.on("click", e => {
            let districtName = e.data.name;
            $this.keyModal = districtName;
            $this.$refs.dialog.query(districtName);
          });

          this.showLoading = false;
        });
    }
  }
};
</script>